package com.serve.oa.mq;

import com.alibaba.fastjson.JSON;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import com.serve.oa.common.lang.Result;
import com.serve.oa.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class regReceiver {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    SysUserService sysUserService;


    @Autowired
    RedisTemplate redisTemplate;
    @RabbitListener(queues = "reg007")
    public void process(@Payload String userId, @Headers Channel channel, Message message) throws InterruptedException, IOException {


        Integer dsa = (Integer) redisTemplate.opsForValue().get("kc001");

        int dld;
        if(dsa==null){
            List<Map> dsd = sysUserService.dsd();
            dld = (int) dsd.get(0).get("stock");
            redisTemplate.opsForValue().set("kc001",dld);

        }else {
            dld = dsa;
        }


        if(dld<=0){

            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

        }else {
            redisTemplate.opsForValue().decrement("kc001");
            sysUserService.emalis(userId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        }


    }


    @RabbitListener(queues = "reg007")
    public void process1(@Payload String userId, @Headers Channel channel, Message message) throws InterruptedException, IOException {



        Integer dsa = (Integer) redisTemplate.opsForValue().get("kc001");

        int dld;
        if(dsa==null){
            List<Map> dsd = sysUserService.dsd();
            dld = (int) dsd.get(0).get("stock");
            redisTemplate.opsForValue().set("kc001",dld);

        }else {
            dld = dsa;
        }


        if(dld<=0){

            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

        }else {
            redisTemplate.opsForValue().decrement("kc001");
            sysUserService.dx(userId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        }






    }



    @PostConstruct
    private void init(){
        /**
         * 消息发送到交换器Exchange后触发回调。
         * 使用该功能需要开启确认，spring-boot中配置如下：
         * spring.rabbitmq.publisher-confirms = true
         */
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {



                if (b) {
                    System.out.println("消息已确认 cause:{"+s+"} - {"+correlationData+"}");
                } else {
                    System.out.println("消息未确认 cause:{"+s+"} - {"+correlationData+"}");
                }
            }
        });
        /**
         * 通过实现ReturnCallback接口，
         * 如果消息从交换器发送到对应队列失败时触发
         * 比如根据发送消息时指定的routingKey找不到队列时会触发
         * 使用该功能需要开启确认，spring-boot中配置如下：
         * spring.rabbitmq.publisher-returns = true
         */
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                log.error("消息被退回:{}", message);
                log.error("消息使用的交换机:{}", exchange);
                log.error("消息使用的路由键:{}", routingKey);
                log.error("描述:{}", replyText);
            }
        });
    }




}
